
import net from './net.js';
import service from './service.js';
import utils from './common/utils.js';  

// #ifdef APP-PLUS||MP-WEIXIN
let uploadItem = null;
const setUploadItem = function(item){
	uploadItem = item;
}

const getUploadItem = function(){
	return uploadItem;
}

const clearUploadItem = function(){
	uploadItem = null;
}

const uploadSampleData = function(isShowLoading = true, isBack = false){
	
	let item = uploadItem;
	console.log(item,"_____item")
	if(isShowLoading === true) {
		uni.showLoading({title: '', mask: true});
	}
	console.log(item);
	try{
		service.ImgtoBase64(item).then((res)=>{
			console.log(res,"up__________________")
			if(res == null || res.length == 0 || Object.keys(res).length == 0) {
				uni.showToast({icon: 'none',title: '该任务数据异常，无法提交'});
				uni.hideLoading();
				return;
			}
			console.log(JSON.stringify(res),"--------");
			let data = encodeURIComponent(JSON.stringify(res));
			console.log(data);
			net.netPost('/module/api/main/upload_sample_data', data, (res)=>{
				if(res == 'success' || (res != null && res.code == 200)) {
					utils.exeMainCallBack({}, '已上传');
					//item.isLoadData = '已上传';
					
					if(isBack) {
						uni.navigateBack({
							delta: 1
						});
					}
				}
			}, (res)=> {
				uni.showToast({icon: 'none',title: '上传失败'});
			}, (res)=> {
				console.log('处理完成___01');
				if(res.statusCode == 200 && res.data == 'success' || (res.data && res.data.code == 200)) {
					uni.showToast({icon: 'none',title: '上传成功'});
				}
			}, undefined, undefined, {'content-type': 'application/json'});
		}).catch(e => {
			uni.hideLoading();
			console.log(e);
			uni.showToast({icon: 'none',title: '程序异常'});
		});
	}catch(e){
		uni.hideLoading();
		console.log(e);
		uni.showToast({icon: 'none',title: '程序异常'});
	}
}

// #endif

// #ifdef  H5 
const setUploadItem = function(item){
	uni.setStorageSync('uploadItem', item);
}
// const setUploadItem =function(item){
// 	 this.$store.dispatch('state.data',item)
// }


const getUploadItem = function(){
	return uni.getStorageSync('uploadItem') || null;
}

const clearUploadItem = function(){
	uni.setStorageSync('uploadItem', null);
}

const uploadSampleData = function(isShowLoading = true, isBack = false){
	let item =  uni.getStorageSync('uploadItem') || null;	
	if(isShowLoading === true) {
		uni.showLoading({title: '', mask: true});
	}
	// console.log("下面是item")
	// console.log(item);
	try{
		service.ImgtoBase64(item).then((res)=>{
			if(res == null || res.length == 0 || Object.keys(res).length == 0) {
				uni.showToast({icon: 'none',title: '该任务数据异常，无法提交'});
				uni.hideLoading();
				return;
			}
			//console.log(JSON.stringify(res));
			let data = encodeURIComponent(JSON.stringify(res));
			//console.log(1111111111111111111111111111111111111);
			//console.log(data);
			net.netPost('/module/api/main/upload_sample_data', data, (res)=>{
				if(res == 'success' || (res != null && res.code == 200)) {
					utils.exeMainCallBack({}, '已上传');
					//item.isLoadData = '已上传';
					if(isBack) {
						uni.navigateBack({
							delta: 1
						});
					}
				}
			}, (res)=> {
				console.log(res)
				uni.showToast({icon: 'none',title: '上传失败'});
			}, (res)=> {
				console.log('处理完成');
				if(res.statusCode == 200 && res.data == 'success' || (res.data && res.data.code == 200)) {
					uni.showToast({icon: 'none',title: '上传成功'});
				}
			}, undefined, undefined, {'content-type': 'application/json'});
		}).catch(e => {
			uni.hideLoading();
			//console.log(e);
			uni.showToast({icon: 'none',title: '程序异常'});
		});
	}catch(e){
		uni.hideLoading();
		//console.log(e);
		uni.showToast({icon: 'none',title: '程序异常'});
	}
}
// #endif


export default {
    uploadSampleData,
	setUploadItem,
	getUploadItem,
	clearUploadItem
}

